<?php

namespace app\api\controller\nc;

use addons\shopro\model\Richtext;
use addons\shopro\model\User;
use addons\shopro\model\UserClaim;
use app\admin\model\nc\Goods;
use app\common\controller\Api;
use think\db\Query;
use think\Queue;

/**
 * 农场首页
 */
class Index extends Api
{
    /**
     * Goods模型对象
     * @var \app\admin\model\nc\Goods
     */
    protected $model = null;
    protected $noNeedLogin = ['index','prefecture','details','search'];
    protected $noNeedRight = ['*'];
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new Goods();
    }
    /**
     * 首页
     *
     */
    public function index(){
        $config = \addons\shopro\model\Config::where(['name' =>'nc'])->value('value');
        $config = json_decode($config,true);
        $config_info['area']   = $config['title'];//占地面积
        $config_info['image']  = $config['image'];//首页背景图
        $config_info['goods_poster_bg'] = $config['goods_poster_bg'];//我得农场背景图
        $config_info['user_num']        = User::count();//用户
        $article        = Richtext::order('updatetime desc')->field('id,title')->select();//新闻公告
        $data['config'] = $config_info;
        $data['articl'] = $article;
        for ($i=0;$i<=1;$i++){
            $type =$i+1;
            if($type==1){
                $key = 'claim';
            }else{
                $key = 'plant';
            }
            $data['nc_goods'][$key] = $this->model->where(['is_hot'=>1,'type'=>$type,'status'=>'up'])->field('id,title,shou_price,type,image,day_earnings,money,like_url')->select();
        }
        $this->success('ok',$data);
    }

    /**
     * 认领@种植专区
     */

    public function prefecture(){
        $type = input('get.type');
        $key  = input('get.key','');
        $where['status'] = 'up';
        if($key){
            $where['title'] = ['like','%'.$key.'%'];
        }
        if($type){
            $where['type'] = $type;
        }
        $data = $this->model->where($where)->field('id,type,title,shou_price,image,day_earnings,kucun,money,like_url')->paginate();
        $this->success('ok',$data);
    }

    /**
     * 详情
     */

    public function details(){
        $ids = input('get.ids');
        if(!$ids){
            $this->error('缺少参数');
        }
        $info = $this->model->where(['id'=>$ids])->find();
        if(!$info){
            $this->error('数据不存在');
        }
        $ly_id = 0;
        $claimModel = new UserClaim();
        if(!empty($this->auth->id)){
            $is_ly = $claimModel->where(['user_id'=>$this->auth->id,'nc_goods_id'=>$ids])->find();
            if($is_ly){
                $ly_id = $is_ly['id'];
            }
        }

//        $info['all_points'] = $info['syday_text'] * $info['day_earnings'];

        $info['member']     = $claimModel->where(['nc_goods_id'=>$ids])->with(['user'=>function(Query $query){
            $query->field('id,avatar');
        }])->limit(3)->field('id,user_id')->select();
        $info['ly_id']      = $ly_id;
        $this->success('ok',$info);
    }

}
